<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/19
 * Time: 11:44
 */

namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;

class Article extends Adminbase
{

    /**
     * 党员干部帮扶中心列表
     * */
    public function index()
    {

        if($this->request->isAjax()) {

            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 10);
            $where = [];
            $order = "id DESC";
            $where[] = ['status','=',1];

            $title = input('title');
            if($title){
                $where[] = ['title','like','%'.$title.'%'];
            }
            $time = input('time');
            if($time){
                $start_time = substr($time, 0, 20);//订单那创建开始时间
                $end_time = substr($time, 21, 20);//订单那创建开始时间
                $time = strtotime($start_time);
                $times = strtotime($end_time);
                $where[] = ['create_time','between',$time.','.$times];
            }
            $level = input('level');
            if($level){
                if($level == 1){
                    $where[] = ['is_boss','=',0];
                }else{
                    $where[] = ['is_boss','=',1];
                }
            }

            $list = Db::name('help_article')->where($where)
                ->page($page,$limit)
                ->order($order)
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->select();
            $isHttp = is_https();
            if($isHttp){
                $http = "https://";
            }else{
                $http = "http://";
            }
            $url = $_SERVER["SERVER_NAME"];

            foreach ($list as $k => $v){

                if($v['pic']){
                    $list[$k]['pic'] = $http.$url."/uploads/images/helpArticle/".$v['pic'];
                }
                if($list[$k]['pid']){
                    $list[$k]['level'] = "二级";
                    $name = Db::name('help_article')->where(['id' => $v['pid']])->value('title');
                    $list[$k]['parent'] = $name;
                }else{
                    $list[$k]['parent'] = "--";
                    $list[$k]['level'] = "一级";
                }
            }

            $total = Db::name('help_article')->where($where)->count();

            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);

        }else{
            return $this->fetch();
        }

    }


    /**
     * 文章添加
     * */
    public function edit()
    {

        if($this->request->isPost()){

            $data = input('post.');
            if(isset($data['isBoss'])){
                if($data['isBoss'] == "on"){
                    $data['is_boss'] = 1;
                }else{
                    $data['is_boss'] = 0;
                }
            }else{
                $data['is_boss'] = 0;
            }
            unset($data['isBoss']);
            if(isset($data['id'])){
                $id = $data['id'];
            }
            unset($data['id']);

            if($data['pics']){
                $pics = array_filter(explode(',',$data['pics']));
                $pics = implode(',',$pics);
                $data['lunbo'] =  $pics;
            }else{
                $data['lunbo'] = "";
            }
            unset($data['pics']);
            unset($data['file']);
            $data['create_time'] = time();

            if($data['is_boss'] == 1){
                if(!$data['pid']){
                    $this->error('请选择上级文章');
                }
            }

            if($id){
                $res = Db::name('help_article')->where(['id' => $id])->update($data);
            }else{
                $res = Db::name('help_article')->insert($data);
            }

            if($res){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }

        }else{

            $id = $this->request->param('id/d');

            if($id){

                $data = Db::name('help_article')->where(['id' => $id])->find();
                $lunbo = [];
                if($data['lunbo']){
                    $lunBoArr = explode(',',$data['lunbo']);
                    foreach ($lunBoArr as $k => $v){
                        $img = Db::name('images')->where(['id' => $v])->field('id,img')->find();
                        $arr = [
                            'id' => $img['id'],
                            'img' => "/uploads/images/helpArticle/".$img['img']
                        ];
                        array_push($lunbo,$arr);
                    }
                }
                $data['pics'] = $lunbo;
                $data['picUrl'] = "/uploads/images/article/".$data['pic'];
            }else{
                $data = [
                    'id' => '',
                    'title' => '',
                    'info' => '',
                    'text' => '',
                    'is_boss' => '',
                    'pid' => '',
                    'pic' => '',
                    'lunbo' => '',
                    'picUrl' => ''
                ];
            }

            $maps[] = ['pid','=',0];
            $list = Db::name('help_article')->where($maps)->field('id,title')->select();
            $data['parent'] = $list;

            $this->assign('data',$data);

            return $this->fetch();
        }

    }



    /**
     * 文章删除
     * */
    public function del()
    {

        $id = input('id');

        if(!$id){
            $this->error('数据获取有误');
        }

        $data['status'] = -1;
        $res = Db::name('help_article')->where(['id' => $id])->update($data);

        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }

    }


    /**
     * 党员干部文章置顶
     * */
    public function setstate()
    {

        if($this->request->isPost()){

            $id = $this->request->param('id/d');
            empty($id) && $this->error('参数不能为空！');
            $isIndex = $this->request->param('isIndex/s') === 'true' ? 1 : 0;

            $data['is_index'] = $isIndex;
            $res = Db::name('help_article')->where(['id' => $id])->update($data);

            $res != false ? true : false;

            if ($res) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }

        }

    }
}